# Background Data
backGrd <- read.csv("~/Dropbox/professional/Research/Submitted/Camp/Data/ChallengerOcc.csv",stringsAsFactors=F)

cand2004 <- read.csv("/users/brucedesmarais/Dropbox/professional/Research/Submitted/Camp/Data/DynamicClustering/ClusteringResultsDyn2004.csv",stringsAsFactors=F)

cand2010 <- read.csv("/users/brucedesmarais/Dropbox/professional/Research/Submitted/Camp/Data/DynamicClustering/ClusteringResultsDyn2010.csv",stringsAsFactors=F)

cand2004$bigClust <- as.numeric(names(table(cand2004$X17))[which.max(table(cand2004$X17))])

cand2010$bigClust <- as.numeric(names(table(cand2010$X17))[which.max(table(cand2010$X17))])

candDat <- rbind(cand2004,cand2010)

candDat$State <- substr(candDat$DistIDRunFor,1,2)
candDat$Dist <- as.numeric(substr(candDat$DistIDRunFor,3,4))
candDat <- subset(candDat,candDat$CRPICO=="C")

epnDat<- subset(candDat,candDat$X17 != candDat$bigClust)

for(i in 1:nrow(backGrd)){
		 backGrd$EPNChal[i] <- sum((epnDat$Dist == backGrd$Dist[i]) &(epnDat$Cycle == backGrd$Year[i])&(epnDat$State == backGrd$State[i]) ) > 0
}

write.csv(backGrd,"~/Dropbox/professional/Research/Submitted/Camp/Data/CandBackGrd.csv",row.names=F)








